package com.lagou.rpc.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @ClassName RpcDecoder
 * @Description 解码器实现
 * @Author wugz
 * @Date 2020/4/19 20:34
 * @Version 1.0
 */
public class RpcDecoder extends MessageToMessageDecoder<ByteBuf> {

    private Class<?> clazz;

    private Serializer serializer;



    public RpcDecoder(Class<?> clazz, Serializer serializer) {
        this.clazz = clazz;
        this.serializer = serializer;
    }

    private volatile int len = 0;


    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
        byte[] req = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(req);
        out.add(serializer.deserialize(clazz,req));
    }

}
